package com.itheima.manage.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.constants.Constants;
import com.itheima.commons.pojo.entity.Admin;
import com.itheima.commons.vo.backstage.AdminVo;
import com.itheima.dubbo.interfaces.AdminApi;
import com.itheima.manage.excepiton.BusinessException;
import com.itheima.manage.excepiton.ErrorResult;
import com.itheima.manage.interceptor.AdminThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class AdminService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Reference
    private AdminApi adminApi;

    //创建图片验证码,将uuid和验证码存入redis
    public byte[] creatVerification(String uuid) {
        //获取验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 30, 4, 15);
        byte[] imageBytes = lineCaptcha.getImageBytes();
        //生成字符串的验证码
        String code = lineCaptcha.getCode();
        System.out.println("验证码为" + code);
        //将uuid和code存入redis中
        redisTemplate.opsForValue().set(uuid, code);
        System.out.println("存入成功");
        return imageBytes;
    }

    //验证账号密码是否正确
    public void login(String username, String password) {
        //根据user去查密码
        String password2 = adminApi.checkPassword(username);
        //如果是空账号不正确
        if (StrUtil.isBlank(password2)) {
            throw new BusinessException(ErrorResult.userNameError());
        }
        //判断密码是否正确
        boolean equals = StrUtil.equals(password, password2);
        if (!equals) {
            throw new BusinessException(ErrorResult.passwordError());
        }

    }

    //校验验证码是否正确
    public void checkCode(String uuid, String verificationCode) {
        String code = redisTemplate.opsForValue().get(uuid);
        //比较验证码是否正确
        boolean equals = StrUtil.equals(code, verificationCode);
        if (!equals) {
            throw new BusinessException(ErrorResult.codeError());
        }else{
            redisTemplate.delete(uuid);
            System.out.println("验证码删除成功");
        }
    }

    //生成token
    public HashMap<String,Object> creatToken(String username) {
        //获得admin的id
        Admin admin = adminApi.getAdmin(username);
        Long id = admin.getId();
        // 生成Token
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("aid", id);
        //过期时间一周
        map.put(JWTPayload.EXPIRES_AT, DateTime.of(System.currentTimeMillis() + Constants.JWT_TIME_OUT));
        //加密
        String token = JWTUtil.createToken(map, Constants.JWT_SECRET.getBytes());
        System.out.println("生成的token为"+token);
        //将token添加到map中
        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        return tokenMap;

    }

    //获取用户基本信息
    public AdminVo getAdmin() {
//        //从线程中获取admin
//        Admin admin = AdminThreadLocal.get();
//        String username = admin.getUsername();
//        //通过username去寻找admin对象
//        Admin admin1 = adminApi.getAdmin(username);
//        //拼凑VO对象
//        AdminVo adminVo = new AdminVo();
//        adminVo.setUid(admin1.getId().toString());
//        adminVo.setUsername(admin1.getUsername());
//        adminVo.setAvatar(admin1.getAvatar());
//        return adminVo;
        Long adminId = AdminThreadLocal.getAdminId();
        Admin admin = adminApi.getAdminByAid(adminId);
        AdminVo adminVo = new AdminVo();
        adminVo.setUid(adminId.toString());
        adminVo.setUsername(admin.getUsername());
        adminVo.setAvatar(admin.getAvatar());
        return adminVo;
    }
}
